package com.ruoyi.system.task;

import com.ruoyi.system.domain.SysCertificateInfo;
import com.ruoyi.system.mapper.SysCertificateInfoMapper;
import com.ruoyi.system.util.DateUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * @ClassName: CertificateStateUpdate
 * @Description: 每日12点定时更新证书状态
 *
 * @author linjinkun
 * @date 2023/10/24
 */
@Component
public class CertificateStateUpdate {

    @Resource
    private SysCertificateInfoMapper sysCertificateInfoMapper;


    @Scheduled(cron = "0 0 0 * * ?")
    public void function() {
        // 定时任务逻辑
        //获取当前日期前一天
        Date date = DateUtils.getMinusDay(1);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = format.format(date);
        java.sql.Date sqlDate = java.sql.Date.valueOf(dateString);
        //数据库中搜索截止时间为当前日期证书
        SysCertificateInfo sysCertificateInfo = new SysCertificateInfo();
        sysCertificateInfo.setCertificateDeadline(sqlDate);
        List<SysCertificateInfo> list = sysCertificateInfoMapper.selectSysCertificateInfoList(sysCertificateInfo);
        //对这些证书的state字段进行修改
        for (SysCertificateInfo i : list) {
            i.setCertificateCheckStatus("失效");
            sysCertificateInfoMapper.updateSysCertificateInfo(i);
        }
    }


}
